<template>
	<!-- 简单导航 -->
	<SimpleNav />
	<div class="find-pwd">
		<el-form :model="form" label-width="auto">
			<h1>找回密码</h1>
			<el-form-item label="邮箱" v-if="!staff" >
				<el-input v-model.trim="form.email" size="large" placeholder="请输入邮箱账号">
					<template #suffix>
						<el-icon :size="26">
							<Message />
						</el-icon>
					</template>
					<template #append>
						<el-button style="width: 100px; background-color: #40abec;color: #f3f3f6" @click="getCode" :disabled="disabled">{{text}}</el-button>
					</template>
				</el-input>
			</el-form-item>
			
			<el-form-item label="验证码" v-if="!staff" >
				<el-input v-model.trim="form.code" size="large" placeholder="请输入验证码" />
			</el-form-item>

			<el-form-item label="手机号" v-if="staff">
				<el-input v-model.trim="form.tel" size="large" placeholder="请输入手机账号">
					<template #suffix>
						<el-icon :size="26">
							<PhoneFilled />
						</el-icon>
					</template>
				</el-input>
			</el-form-item>

			<el-form-item label="真实姓名" v-if="staff">
				<el-input v-model.trim="form.name" type="text" size="large" placeholder="请输入真实姓名">
					<template #suffix>
						<el-icon :size="26">
							<User />
						</el-icon>
					</template>
				</el-input>
			</el-form-item>

			<el-form-item label="新密码">
				<el-input v-model.trim="form.password" type="password" size="large" placeholder="请输入新密码">
					<template #suffix>
						<el-icon :size="26">
							<Unlock />
						</el-icon>
					</template>
				</el-input>
			</el-form-item>

			<el-form-item label="确认密码">
				<el-input v-model.trim="form.password2" type="password" size="large" placeholder="请再次输入密码">
					<template #suffix>
						<el-icon :size="26">
							<Unlock />
						</el-icon>
					</template>
				</el-input>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" size="large" style="width: 100%;" @click="handleAction">找回密码</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import SimpleNav from '@/components/SimpleNav.vue'
	export default {
		components: {
			SimpleNav,
		},
		props: {
			is_staff: {
				type: String,
				required: true,
			}
		},
		data() {
			return {
        disabled:false, //是否禁用
        text:"获取验证码", // 提示信息
				form: {
					tel: '',
					name: '',
					code: '',
					email: '',
					password: '',
					password2: '',
				}
			}
		},
		computed: {
			staff() {
				return atob(this.is_staff) === 'enterprise'
			}
		},
		methods: {
      getCode(){
        //获取普通用户邮箱验证码
        if(this.form.email===""){
          this.$layer.msg("邮箱不能为空")
          return false;
        }
        if(!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.form.email)){
          this.$layer.msg("邮箱账户格式不正确")
          return false;
	 	    }
        //发送异步请求,并设置60秒钟倒计时
        this.disabled =true;
        this.text  = 10;
        //使用轮询,完成60秒倒计时
        let playing =setInterval(()=>{
          --this.text;
          if(this.text<= 0){
            this.text="获取验证码";
            this.disabled=false;
            clearInterval(playing)
          }
        },1000)
        //发送异步请求,获取验证码
        this.$api.getCodeByEmail(this.form.email).then(ret=>{
          this.$layer.msg('验证码已发送,请及时查看邮箱');
          return false;
        }).catch(error=>{
          this.$layer.msg(error.response.data.message);
          return false;
        })

      },
			handleAction() {
        //找回密码
        if(this.form.password===""){
	  	    this.$layer.msg("密码不允许为空")
	  	    return;
	      }

	    if(this.form.password !== this.form.password2){
	  	    this.$layer.msg("两次输入的密码不一致")
	  	    return;
	      }

	    // // 校验是否设置了真实姓名
	    // if (this.form.name ===""){
	  	//     this.$layer.msg("姓名不能为空")
	  	//     return;
	    //   }
        this.$api.handleActions(this.form).then(ret=>{
          this.$layer.msg(ret.message)
          return;
        })
			},
		}
	}
</script>

<style scoped>
	.find-pwd {
		width: 900px;
		height: 560px;
		background-color: #f0f0f0;
		display: flex;
		border-radius: 20px 0 0 20px;
		margin: calc(50vh - 280px) auto;
		padding: 30px;
	}

	.find-pwd .el-form {
		flex: 1 1 auto;
	}

	.find-pwd h1 {
		height: 60px;
		border-bottom: 3px solid #fff;
		margin: 30px 0;
	}
</style>